'use strict';

const Controller = require('egg').Controller;
const stringRandom = require('string-random');
const sha1 = require("sha1");
class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    await ctx.render('/recording');
  }
  async pay(){
    const { ctx } = this;
    await ctx.render('/pay');
  }

  //获取
  async getJsSdk(){
    const { ctx } = this;
    let access_token=''
    let ticket=''
    let appId='wxa97639ce5ff70793'
    let {url} = this.ctx.query
    if(ctx.session.getJsSdk){
      access_token = ctx.session.getJsSdk.access_token
      ticket = ctx.session.getJsSdk.ticket
    }else{
   const access_token  = await this.service.jsSdk.getJsSdk(url)
     ticket = await this.service.jsSdk.getTicket(access_token)
    }
   let nonceStr = stringRandom(16, { numbers: true });
   let timestamp = new Date().getTime()
  //  const href = this.ctx.href
  //  href.substr(0,href.indexOf('getJsSdk'))
   const string1= `jsapi_ticket=${ticket}&noncestr=${nonceStr}&timestamp=${timestamp}&url=${url}`
   const signature = sha1(string1)
  if(ticket){
    ctx.session.getJsSdk={access_token,ticket}
    ctx.body={
      appId,
      nonceStr,
     timestamp,
     signature,
    }
  }else{
     ctx.body={
      err:202
  }
  }
  }
  async jssdkVoice(){
    const { ctx } = this;
    const mediaId = ctx.queries.mediaId[0]
    const writing = await this.service.jsSdk.jssdkVoice(mediaId)
  }
}

module.exports = HomeController;
